layui.use(['layer','treeSelect','table','form','laytpl','element'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,table = layui.table
        ,element = layui.element
        ,form = layui.form
        ,laytpl = layui.laytpl;
    var delFlag = null;
    var treeSelect= layui.treeSelect;//树形下拉框

    //树形下拉分类
    treeSelect.render({
        elem: '#tree-select',
        data: '/api/agt_tag/tree/select',
        type: 'get',
        placeholder: '父级分类',// 占位符
        search: true,// 是否开启搜索功能：true/false，默认false
        click: function(data){
            $("#tree-select").val(data.current.id);
        },
        success: function (data) { // 加载完成后的回调函数
            $("#tree-select").val("0");
        }
    });

    /*form.on('switch(switchTest)', function(data){
        if(this.checked) {
            $("#isShow").val('1');
            data.value="1";
        }else {
            $("#isShow").val('0');
            data.value="0";
        }
        layer.tips('温馨提示：开启后分类会在前台显示，关闭则不会在前台显示', data.othis)
    });*/

    //添加产品分类信息
    form.on('submit(edit)', function(data){
        if (data.field.isShow == null || data.field.isShow === "") {
            data.field.isShow = "0";
        }
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            url: '/api/agt_tag/add',
            data: data.field,
            type:'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("添加失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("添加失败！");
            }
        });
        return false;
    });

});

